import vue from '@vitejs/plugin-vue2'
import { defineConfig,loadEnv } from 'vite'
import babel from 'vite-plugin-babel';
import viteCompression from 'vite-plugin-compression' //静态资源压缩

export default defineConfig((mode, command) => {
  const env = loadEnv(mode, process.cwd(), '')
  return {
    plugins: [
      vue(),
      babel(),
      viteCompression({
        verbose: true, // 默认即可
        disable: false, // 开启压缩(不禁用)，默认即可
        deleteOriginFile: false, // 删除源文件
        threshold: 5120, // 压缩前最小文件大小
        algorithm: 'gzip', // 压缩算法
        ext: '.gz' // 文件类型
      }),

  ],
    build: {
      rollupOptions: {
           output: {
        // 静态资源打包做处理
        chunkFileNames: 'static/js/[name]-[hash].js',
        entryFileNames: 'static/js/[name]-[hash].js',
        assetFileNames: 'static/[ext]/[name]-[hash].[ext]',
          manualChunks(id) {
            if (id.includes('node_modules')) {
              return id.toString().split('node_modules/')[1].split('/')[0].toString();
            }
          }
        }
      },
    outDir: './dist',
    assertsDir: 'static',
        terserOptions: {//terser
        compress: {
          drop_console: env.APP_ENV==='production',
          drop_debugger: env.APP_ENV=='production',
        }
      },
  },
  base:"./",
  resolve: {
     alias: {
        'vue$': 'vue/dist/vue.esm.js',
        '@': '/src',
        '@assets': '/src/assets',
        '@components': '/src/components',
        '@views': '/src/views',
        '@store': '/src/store',
        '@utils': '/src/utils',
      },
  },

  server: {
    proxy: {
      '/api': {
        target: '',  //设置跨域访问的网址
        changeOrigin: true,   // 允许跨域
        rewrite: (path) => path.replace(/^\/api/, '') // 重写路径把路径变成空字符
      }
    }
  }
  }

})